package models;

import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;
import play.*;
import play.data.validation.MaxSize;
import play.data.validation.Valid;
import play.db.jpa.*;
import javax.persistence.*;
import java.util.*;

/**
 * 输变电工程合同信息
 * 
 */

@Entity 
public class Project extends Model {

    @ManyToOne
    public Dept dept;    //单位名称
    
    @ManyToOne
    public User user;

    public String name;            //项目名称

    public String contractUnit;    //合同单位
    
    public double contractAmount;  //合同总价

    public double paidIn;          //累计到位资金

    public double appropriate;     //累计拨付资金

    public double paidInDiff;      //到位资金差额

    public double appropriateDiff; //拨付资金差额

    public String  remark;  //备注
    
    @OneToMany
    public List<Contract> contracts;
    
 
    public String toString() {
        return name;
    }
        
    public static Project findByUser(User user,int year) {
       return find("byUserAndYear", user, year).first();
    }
    
    public static List<Project> serachByNameContractUnit(String name,String contractUnit,Dept dept){
    	String sql = "dept=? and name like '%#name%' and contractUnit like '%#contractUnit%'";
    	sql = name==null||"".equals(name)?sql.replace("and name like '%#name%'", "")
    			:sql.replace("#name", name);
    	sql = contractUnit==null||"".equals(contractUnit)?sql.replace("and contractUnit like '%#contractUnit%'", "")
    			:sql.replace("#contractUnit", contractUnit);
    	return Project.find(sql,dept).fetch();
    }

}